<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;
use app\common\model\Newscat;
use app\common\model\News as NewsModel;

class News extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {

        $where = [];
        $keyword = input('title');
        if(!empty($keyword)){
            $where['title'] = ['like', "%$keyword%"];
        }
        $op=input('op');
        if($op=='cate')
        {
            $catid=input('id');
            if(!empty($catid)){
                $where['catid'] = intval($catid);
            }
        }

        $newscat=Newscat::select();
        $this->assign('newscat',$newscat);
        $list = model('News')::where($where)->order('displayorder desc')->paginate(5);
        $this->assign('list', $list);

        $news=new NewsModel();
        if(request()->isPost()){
            $displayorder=input('post.');
            foreach ($displayorder as $k => $v) {
                $news->update(['id'=>$k,'displayorder'=>$v]);
            }
            $this->success('更新排序成功！',url('index'));
            return;
        }
        return view('newslist');
    }
    public function stop($id)
    {
        $news =model('News')::find($id);
        if($news['status']==1){
            Db::table('fx_news')
                ->where('id', $id)
                ->update(['status' => 0]);
            $this->success('隐藏成功', 'index');
        }else{
            Db::table('fx_news')
                ->where('id', $id)
                ->update(['status' => 1]);
            $this->success('显示成功', 'index');
        }




    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {

        $newscat=Newscat::select();
        $this->assign('newscat',$newscat);
        return $this->fetch('add2');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
       $data=input('post.');
        $type =$_FILES['face']['type'];
        $file = request()->file('face');
        if (empty($file)) {
            //必须上传商品logo图片
            $this->fail('请上传文件');
        }
        //将文件移动到指定的目录（public 目录下  uploads目录 goods目录）
        $dir = ROOT_PATH . 'public' . DS . 'uploads' . DS . $type;
        if(!is_dir($dir)) mkdir($dir,0777,true);;
        $info = $file->validate(['size' => 10*1024*1024, 'ext' => ['jpg', 'png', 'gif', 'jpeg']])->move($dir);
        if (empty($info)) {
            //上传出错
            $msg = $file->getError();
            $this->fail($msg);
        }
        //拼接图片的访问路径
        $data['headimg'] = DS . "uploads" . DS . $type . DS . $info->getSaveName();
        $data['content']=$data['editorValue'];
        //入库操作
        $id = model('News')->create($data, true);;

        if($id) {
            $this->success('新增成功', 'index');
        } else {
            $this->error('新增失败', 'index');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $news=NewsModel::find($id);
        $newscat=Newscat::select();
        $this->assign('newscat',$newscat);
        $this->assign('news',$news);
        return $this->fetch('edit');
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $data = input('post.');
        if($_FILES['face']['tmp_name'])
        {
            $type =$_FILES['face']['type'];
            $file = request()->file('face');

            //将文件移动到指定的目录（public 目录下  uploads目录 goods目录）
            $dir = ROOT_PATH . 'public' . DS . 'uploads' . DS . $type;
            if(!is_dir($dir)) mkdir($dir,0777,true);;
            $info = $file->validate(['size' => 10*1024*1024, 'ext' => ['jpg', 'png', 'gif', 'jpeg']])->move($dir);
            if (empty($info)) {
                //上传出错
                $msg = $file->getError();
                $this->fail($msg);
            }
            //拼接图片的访问路径
            $data['headimg'] = DS . "uploads" . DS . $type . DS . $info->getSaveName();
            $data['content']=$data['editorValue'];

            model('news')::update($data,['id' => $data['id']], true);
        }else{
            model('news')::update($data,['id' => $data['id']], true);
        }
        $this->success('修改成功', 'index');
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {

        $op=input('op');
        if($op='delall')
        {
            $ids=input("id");
//          if(Db::name("News")->where("id in ($ids)")->delete())
//          {
//              return json(['code' => '200', 'msg' => '操作成功']);
//          }
            return json(['code'=>'0','msg'=>$ids]);

        }

        NewsModel::destroy($id);

        return json(['code' => '200', 'msg' => '操作成功']);
    }
}
